restitutionZoneOperator = operator;
restitutionZones = [];
restitutionZoneMap = null;


function loadRestitutionZones(restitutionZoneMap_, operator) {
    adminReport('Chargement des zones de restitution ');
    restitutionZoneMap = restitutionZoneMap_;
    restitutionZoneOperator = operator;
    $.get("rest/restitutionZone/findall?operator=" + restitutionZoneOperator, function(data) {
        var result = JSON.parse(data);
        for (var i = 0; i < result.length; i++) {
            var zone = result[i];
            restitutionZones.push(addMarkerRestitutionZone({
                param: JSON.parse(zone.ZAR_param),
                actif: zone.ZAR_actif,
                id: zone.ZAR_id,
                reference: zone.ZAR_reference
            }));
        }
        load_restitution_table_reporting(restitutionZones);
    });
}


function saveRestitutionZone(zone) {
    adminReport("Sauvegarde de la Zone de restitution");
    zone.save(restitutionZoneOperator, function() {

    });
}
function drawBoundsRestitutionZone() {
    for (var i = 0; i < restitutionZones.length; i++) {
        restitutionZones[i].setMap(restitutionZoneMap);
    }
}

function clearMarkerRestitutionZone() {
    for (var i = 0; i < restitutionZones.length; i++) {
        restitutionZones[i].clearMap();
    }
}

/**
 *  Add new bound to an existing GZ - call from external cmd
 */
function newMarkersRestitutionZone(lat, lng) {
    var zone = addMarkerRestitutionZone({
        param: {
            'lat': lat,
            'lng': lng}
        ,
        actif: 1,
        id: null,
        reference: 'RZ' + restitutionZones.length
    });
    debugger;
    restitutionZones.push(zone);
    onRestitutionZoneChanged(zone);
}

function addMarkerRestitutionZone(info) {
    console.log('Chargement de la zone de restitution ' + info.reference + ' Lat=' + info.param.lat + ' Lng=' + info.param.lng);
    var marker = new google.maps.Marker({
        position: createPoint(info.param.lat, info.param.lng),
        map: restitutionZoneMap,
        draggable: true,
        title: info.reference,
        mapTypeId: google.maps.MapTypeId.SATELLITE
    });
    marker.setIcon('/supervision/images/target.png');
    marker.setMap(restitutionZoneMap);
    var zone = new RestitutionZone(marker, info);
    google.maps.event.addListener(marker, 'click', function() {
        if (confirm('Suppression de la zone ?')) {
            var index = restitutionZones.indexOf(zone);
            if (index > -1) {
                restitutionZones.splice(index, 1);
            }
            marker.setMap(null);
            onRestitutionZoneChanged(zone);
        }
    });
    google.maps.event.addListener(marker, 'dragend', function() {
        var pos = zone.marker.position;
        var lat = nThObject(pos, 1);
        var lng = nThObject(pos, 2);
        zone.info.param.lat=lat;
        zone.info.param.lng=lng;
        onRestitutionZoneChanged(zone);
    });

    return zone;
}


function nThObject(obj, n) {
    var t = n;
    for (var attr in obj) {
        if (t === 1) {
            return obj[attr];
        } else {
            t--;
        }
    }
}

function onRestitutionZoneChanged(zone) {
    saveRestitutionZone(zone);
    load_restitution_table_reporting(restitutionZones);
} 